<template>
    <div class="container-content">
        <div class="container-content-title">{{ $t('platform.setting.config.title') }}-开关配置
            <el-button type="text" class="p-0" @click="dialogUpdate" v-if="dataStatus == 'dle'">{{ $t('action.edit') }}</el-button>
        </div>
        <component-page-loading :status="dataStatus" v-if="['loading', 'error'].indexOf(dataStatus) >= 0" @reload="getData"></component-page-loading>
        <el-form label-width="180px" class="detail" v-else>
            <el-form-item :label="$t('platform.config.rtpSwitch.rtpControlPersonal')">
                {{ $te(`platform.config.rtpSwitchList.${data.rtp_control_personal}.label`) ? $t(`platform.config.rtpSwitchList.${data.rtp_control_personal}.label`) : '' }}
            </el-form-item>
            <el-form-item :label="$t('platform.config.rtpSwitch.rtpControlNew')">
                {{ $te(`platform.config.rtpSwitchList.${data.rtp_control_new}.label`) ? $t(`platform.config.rtpSwitchList.${data.rtp_control_new}.label`) : '' }}
            </el-form-item>
<!--            <el-form-item :label="$t('platform.config.rtpSwitch.rtpPeriod')">-->
<!--                {{ $te(`platform.config.rtpSwitchList.${data.rtp_period}.label`) ? $t(`platform.config.rtpSwitchList.${data.rtp_period}.label`) : '' }}-->
<!--            </el-form-item>-->

        </el-form>
        <el-dialog :title="$t('platform.setting.config.dialog.update')" width="560px" :visible.sync="dialog.visible.update">
            <div class="pr-12">
                <el-form :model="currencyCache" label-width="150px">
                    <el-form-item :label="$t('platform.config.rtpSwitch.rtpControlPersonal')" :error="Boolean(msg.errors['rtp_control_personal']) ? msg.errors['rtp_control_personal'][0] : ''">
                        <el-radio v-model="currencyCache.rtp_control_personal" :label="1">
                            {{$t('platform.config.rtpSwitchList[1].label')}}
                        </el-radio>
                        <el-radio v-model="currencyCache.rtp_control_personal" :label="2">
                            {{$t('platform.config.rtpSwitchList[2].label')}}
                        </el-radio>
                    </el-form-item>


                    <el-form-item :label="$t('platform.config.rtpSwitch.rtpControlNew')" :error="Boolean(msg.errors['rtp_control_new']) ? msg.errors['rtp_control_new'][0] : ''">
                        <el-radio v-model="currencyCache.rtp_control_new" :label="1">
                            {{$t('platform.config.rtpSwitchList[1].label')}}
                        </el-radio>
                        <el-radio v-model="currencyCache.rtp_control_new" :label="2">
                            {{$t('platform.config.rtpSwitchList[2].label')}}
                        </el-radio>
                    </el-form-item>


<!--                    <el-form-item :label="$t('platform.config.rtpSwitch.rtpPeriod')" :error="Boolean(msg.errors['rtp_period']) ? msg.errors['rtp_period'][0] : ''">-->
<!--                        <el-radio v-model="currencyCache.rtp_period" :label="1">-->
<!--                            {{$t('platform.config.rtpSwitchList[1].label')}}-->
<!--                        </el-radio>-->
<!--                        <el-radio v-model="currencyCache.rtp_period" :label="2">-->
<!--                            {{$t('platform.config.rtpSwitchList[2].label')}}-->
<!--                        </el-radio>-->
<!--                    </el-form-item>-->

                </el-form>
            </div>
            <div slot="footer">
                <el-button @click="dialog.visible.update = false">{{ $t('action.cancel') }}</el-button>
                <el-button type="primary" @click="customCurrency">{{ $t('action.confirm') }}</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
export default {
    name: "PlatformRtpSwitch",
    props: ['tree'],
    data() {
        return {
            value1: false,
            currencyListStatus: 'dle',
            currencyList: [],
            currencyCache: {
                rtp_control_personal:2,
                rtp_control_new:2,
                // rtp_period:2,
            },
            currencyInit: {},
            currencyRefresh: false,
            dataStatus: 'dle',
            data: {
                plat_code: null,
                rtp_control_personal:null,
                rtp_control_new:null,
                // rtp_period:null,
            },
            dataCache: {},
            dataInit: {},
            dialog: {
                visible: {
                    update: false,
                }
            },
            msg: {
                code: 200,
                message: '',
                errors: {},
            },
        }
    },
    watch: {
        tree: {
            deep: true,
            immediate: true,
            handler(n, o) {
                // 获取数据
                n.id != null && this.getData();
            }
        },
    },
    created() {
        // 备份初始数据
        this.currencyInit = _.cloneDeep(this.currencyCache);
        this.dataInit = _.cloneDeep(this.data);
        // 获取币种列表
        this.getCurrencyList();
    },
    methods: {
        // 获取币种列表
        getCurrencyList() {
            this.currencyListStatus = 'loading';
            axios.get('/common/getCurrencyList', {
                params: {system: true}
            }).then((response) => {
                if (response.data.resp_msg.code == 200) {
                    this.currencyList = response.data.resp_data;
                    this.currencyListStatus = 'dle';
                } else {
                    this.currencyListStatus = 'error';
                }
            })
        },
        // 获取数据
        getData() {
            this.dataStatus = 'loading';
            axios.get('/platform/setting/config/getData', {
                params: {
                    plat_code: this.tree.code
                }
            }).then((response) => {
                if (_.includes([200, 44100], response.data.resp_msg.code)) {
                    this.data = response.data.resp_data || _.cloneDeep(this.dataInit);
                    this.dataStatus = 'dle';
                    // 广播事件到父组件
                    this.$emit('update', this.data);
                } else {
                    this.dataStatus = 'error';
                }
            })
        },
        initMsg() {
            this.msg.code = 200;
            this.msg.message = '';
            this.msg.errors = {};
        },
        // 显示模态框（编辑）
        dialogUpdate() {
            this.initMsg();
            // 缓存数据
            this.currencyCache = _.cloneDeep(this.data);
            this.dataCache = _.cloneDeep(this.data);
            // 平台code
            this.dataCache.plat_code = this.tree.code;

            // 显示模态框
            this.dialog.visible.update = true;
        },
        customCurrency() {
            this.initMsg();
            // loading 状态 start
            let loading = this.$loading();
            console.log(this.currencyCache)
            // 保存数据
            axios.post('/platform/setting/config/updateOrCreateRtp', this.currencyCache).then((response) => {
                // loading 状态 close
                loading.close();
                // 逻辑处理
                if (response.data.resp_msg.code == 200) {
                    this.$message({
                        type: 'success',
                        message: this.$t('messages.succeeded', {status: this.$t('action.add')}),
                        showClose: true
                    });
                    // 更新数据
                    this.data = response.data.resp_data;
                    // 关闭模态框
                    this.dialog.visible.update = false;
                    // 广播事件到父组件
                    this.$emit('update', this.data);
                } else if (response.data.resp_msg.code == 42000) {
                    this.$message({
                        type: 'error',
                        message: response.data.resp_msg.message,
                        showClose: true
                    });
                    this.msg = response.data.resp_msg;
                } else {
                    this.$message({
                        type: 'error',
                        dangerouslyUseHTMLString: true,
                        message: this.$t('messages.failed', {status: this.$t('action.add')}) + `<div class="mt-2">${response.data.resp_msg.message}</div>`,
                        showClose: true
                    });
                }
            })
        },
        onSubmit() {
            this.initMsg();
            // loading 状态 start
            let loading = this.$loading();
            // 保存数据
            axios.post('/platform/setting/config/updateOrCreateRtp', this.dataCache).then((response) => {
                // loading 状态 close
                loading.close();
                // 逻辑处理
                if (response.data.resp_msg.code == 200) {
                    this.$message({
                        type: 'success',
                        message: this.$t('messages.succeeded', {status: this.$t('action.edit')}),
                        showClose: true
                    });
                    // 更新数据
                    this.data = response.data.resp_data;
                    // 关闭模态框
                    this.dialog.visible.update = false;
                    // 广播事件到父组件
                    this.$emit('update', this.data);
                } else if (response.data.resp_msg.code == 42000) {
                    this.$message({
                        type: 'error',
                        message: response.data.resp_msg.message,
                        showClose: true
                    });
                    this.msg = response.data.resp_msg;
                } else {
                    this.$message({
                        type: 'error',
                        dangerouslyUseHTMLString: true,
                        message: this.$t('messages.failed', {status: this.$t('action.edit')}) + `<div class="mt-2">${response.data.resp_msg.message}</div>`,
                        showClose: true
                    });
                }
            })
        },
    }
}
</script>

<style scoped>
::v-deep .el-dialog__body {
    padding-top: 15px;
    padding-bottom: 15px;
}
</style>
